package br.com.almoxarifadoporttek.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.almoxarifadoporttek.model.Historico;
import br.com.almoxarifadoporttek.model.Local;
import br.com.almoxarifadoporttek.model.Produto;
import br.com.almoxarifadoporttek.model.Usuario;
import br.com.almoxarifadoporttek.util.HibernateUtil;

public class LocalDAO implements ILocalDAO{

	@Override
	public List<Local> list() {

Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			//List lista = session.createQuery("from Livro").list();		
			 
			List<Local> lista = session.createCriteria(Local.class).list();
			t.commit();
			return lista;
			
		}
		catch(Exception erro)
		{
			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
				}
			
				
			}
			catch (Exception e) 
			{
				
			}
			
			return null;			
		}
		
		finally 
		{
			session.close();		
		}	
		
	}


	
	@Override
	public void save(Local local) {
		
			Session session = null;
			try
			{
				session= HibernateUtil.getSession().openSession();
			
				Transaction t = session.beginTransaction();
				session.save(local);
				t.commit();
				
			}catch(Exception erro){			
				try
				{			
					if(session != null)
					{
						if(session.getTransaction().isActive())
							session.getTransaction().rollback();				
					}				
				} catch (Exception e) {
					
				}		
			}		
			finally 
			{
				session.close();	
			}
			
		}
	

	@Override
	public void delete(Local local) {
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.delete(local);
			t.commit();
			
		}catch(Exception erro){			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} catch (Exception e) {
				
			}		
		}		
		finally 
		{
			session.close();	
		}
		
		
	}

	@Override
	public void update(Local local) {
		Session session = null;
		try
		{
			session= HibernateUtil.getSession().openSession();
		
			Transaction t = session.beginTransaction();
			session.update(local);
			t.commit();
			
		}catch(Exception erro){			
			try
			{			
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();				
				}				
			} catch (Exception e) {
				
			}		
		}		
		finally 
		{
			session.close();	
		}
		
	}



	@Override
	public Local getLocal(long id) 
	{
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();			
			Local local = (Local) session.get(Local.class, id);		
			
			return local;
		}finally 
		{
			
			try
			{			
				if(session != null)
				{
						session.close();
				}
			
				
			} catch (Exception e) {
				
			}
		
		}
	}



	@Override
	public List<Local> listByIdInicialIdFinal(long idInicial, long idFinal)
	{
		Session session = null;
		try
		{
		session = HibernateUtil.getSession().openSession();
		Transaction t = session.beginTransaction();
		Criteria criteria = session.createCriteria(Local.class);		
		criteria.add(Restrictions.between("id", idInicial, idFinal));
		List<Local> lista = criteria.list();
	
		
		t.commit();
		return lista;
		
	}
	catch(Exception erro)
	{			
		
		try
		{			
			if(session != null)
			{
				if(session.getTransaction().isActive())
					session.getTransaction().rollback();				
			}				
		} 
		catch (Exception e) 
		{}	
		
		return null;
	}		
	finally 
	{
		session.close();	
	}
	
	}

}
